package com.offer;


import com.utils.DataUtils;

import java.util.Comparator;
import java.util.PriorityQueue;

public class Test59_1 {

    public int[] maxSlidingWindow(int[] nums, int k) {
        if (k == 0) {
            return new int[k];
        }

        if (nums.length < k) {
            return new int[k];
        }

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        int[] result = new int[nums.length - k + 1];

        for (int i = 0; i < k; i++) {
            priorityQueue.offer(nums[i]);
        }

        result[0] = priorityQueue.peek();

        for (int i = k; i < nums.length; i++) {
            priorityQueue.remove(nums[i - k]);
            priorityQueue.offer(nums[i]);
            result[i - k + 1] = priorityQueue.peek();
        }

        return result;
    }

    public static void main(String[] args) {
        Test59_1 test = new Test59_1();

        DataUtils.printArray(test.maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3));
    }

}
